﻿// © 2009 Michele Leroux Bustamante. All rights reserved. 
// See http://wcfguidanceforwpf.codeplex.com for related whitepaper and updates
// For an intro to WCF see Michele's book: Learning WCF, O'Reilly 2007 (updated August 2008 for VS 2008)
// See http://www.thatindigogirl.com for the book code!

using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Runtime.Serialization;
using System.Text;

namespace Entities
{
    [DataContract(Namespace = "http://wcfclientguidance.codeplex.com/2009/04/schemas")]
    public partial class TodoItem : INotifyPropertyChanged
    {
        private string _Title;
        private DateTime? _DueDate;
        private string _Description;
        private PriorityFlag _Priority;
        private StatusFlag _Status;
        private DateTime? _CreationDate;
        private DateTime? _CompletionDate;
        private double _PercentComplete;
        private string _Tags;


        [DataMember(Order = 1, IsRequired = false)]
        public virtual string ID { get; set; }
        [DataMember(Order = 2, IsRequired = true)]
        public virtual string Title
        {
            get { return _Title; }
            set
            {
                if (string.IsNullOrEmpty(value))
                    throw new Exception("Title cannot be empty");
                _Title = value;
                NotifyPropertyChanged("Title");
            }
        }

        [DataMember(Order = 3, IsRequired = true)]
        public virtual string Description
        {
            get { return _Description; }
            set
            {
                _Description = value;
                NotifyPropertyChanged("Description");
            }
        }

        [DataMember(Order = 4, IsRequired = true)]
        public virtual PriorityFlag Priority
        {
            get { return _Priority; }
            set
            {
                _Priority = value;
                NotifyPropertyChanged("Priority");
            }
        }

        [DataMember(Order = 5, IsRequired = true)]
        public virtual StatusFlag Status
        {
            get { return _Status; }
            set
            {
                _Status = value;
                NotifyPropertyChanged("Status");
            }
        }

        [DataMember(Order = 6, IsRequired = true)]
        public virtual DateTime? CreationDate
        {
            get { return _CreationDate; }
            set
            {
                if (CreationDate != null && value > DueDate)
                    throw new Exception("Creation date cannot be after Due date");
                _CreationDate = value;
                NotifyPropertyChanged("CreationDate");
            }
        }

        [DataMember(Order = 7, IsRequired = true)]
        public virtual DateTime? DueDate
        {
            get { return _DueDate; }
            set
            {
                if (value != null && value < CreationDate)
                    throw new Exception("Due date cannot be before creation date");
                _DueDate = value;
                NotifyPropertyChanged("DueDate");
            }
        }

        [DataMember(Order = 8, IsRequired = true)]
        public virtual DateTime? CompletionDate
        {
            get { return _CompletionDate; }
            set
            {
                _CompletionDate = value;
                NotifyPropertyChanged("CompletionDate");
            }
        }

        [DataMember(Order = 9, IsRequired = true)]
        public virtual double PercentComplete
        {
            get { return _PercentComplete; }
            set
            {
                if (_PercentComplete == 100 && value < 100)
                    _CompletionDate = null;
                if (value > 100)
                    throw new Exception("Percent value should be between 0 and 100");
                _PercentComplete = value;
                if (_PercentComplete == 100)
                    CompletionDate = DateTime.Now;
                NotifyPropertyChanged("PercentComplete");
            }
        }

        [DataMember(Order = 10, IsRequired = true)]
        public virtual string Tags
        {
            get { return _Tags; }
            set
            {
                _Tags = value;
                NotifyPropertyChanged("Tags");
            }
        }


        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
        #endregion

    }

    public enum PriorityFlag
    {
        Low,
        Normal,
        High
    }

    public enum StatusFlag
    {
        NotStarted,
        InProgress,
        Completed,
        WaitingOnSomeoneElse,
        Deferred
    }
}
